package com.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.sql.Statement;

public class ConnectionGateway {
	public static Connection connection = null;

	public static boolean Connect() {

		Statement statement = null;
		try {

			// Load the HSQL Database Engine JDBC driver
			// hsqldb.jar should be in the class path or made part of the
			// current jar
			Class.forName("org.hsqldb.jdbc.JDBCDriver");

			// connect to the database. This will load the db files and start
			// the
			// database if it is not alread running.
			// db_file_name_prefix is used to open or create files that hold the
			// state
			// of the db.
			// It can contain directory names relative to the
			// current working directory
			connection = DriverManager.getConnection("jdbc:hsqldb:"
					+ "newDB", // filenames
					"SA", // username
					""); // password

			connection.setAutoCommit(true);
			return true;
			//
			// statement = connection.createStatement();
			//
			// String update1 =
			// "UPDATE employees SET email = 'a@b.com' WHERE email = 'a@a.com'";
			// statement.executeUpdate(update1);
			// Savepoint savepoint1 = connection.setSavepoint("savepoint1");
			//
			// String update2 =
			// "UPDATE employees SET email = 'b@b.com' WHERE email = 'b@c.com'";
			// statement.executeUpdate(update2);
			// Savepoint savepoint2 = connection.setSavepoint("savepoint2");
			//
			// String update3 =
			// "UPDATE employees SET email = 'c@c.com' WHERE email = 'c@d.com'";
			// statement.executeUpdate(update3);
			// Savepoint savepoint3 = connection.setSavepoint("savepoint3");
			//
			// String update4 =
			// "UPDATE employees SET email = 'd@d.com' WHERE email = 'd@e.com'";
			// statement.executeUpdate(update4);
			// Savepoint savepoint4 = connection.setSavepoint("savepoint4");
			//
			// String update5 =
			// "UPDATE employees SET email = 'e@e.com' WHERE email = 'e@f.com'";
			// statement.executeUpdate(update5);
			// Savepoint savepoint5 = connection.setSavepoint("savepoint5");
			//
			// connection.rollback(savepoint3);
			// connection.commit();

		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
		}
	}

	public static void DisConnect() {
		if (connection != null) {
			try {
				connection.close();
			} catch (SQLException e) {
			}
		}
	}
}
